package com.wslds.blog.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.action.BaseAction;
import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.SessionUtil;
import com.wslds.bean.Type;
import com.wslds.blog.service.TypeService;

@SuppressWarnings("serial")
@Component("typeAction")
@Scope("prototype")
public class TypeAction extends BaseAction{

	private Type type;
	
	@Resource
	private TypeService typeService;
	
	public String refreshTypeCache(){
		
		Type typeParam = new Type();
		List<Type> typeList = typeService.getDao().find("type", typeParam);
		Map<Long,Type> typeMap = new HashMap<Long,Type>();
		for( Type type: typeList ){
			typeMap.put(type.getId(), type);
		}
		SessionUtil.getServletContext().setAttribute(BlogCons.APPLICATION.TYPE, typeMap);
		
		return SUCCESS;
	}
	
	public String listType(){
		PageAdapter<Type> adapter = typeService.getDao().page("type", type, getPage());
		this.put("adapter", adapter);
		return SUCCESS;
	}
	
	public String listAjaxType(){
		PageAdapter<Type> adapter = typeService.getDao().page("type", type, getPage());
		this.put("adapter", adapter);
		return SUCCESS;
	}
	
	public String insert(){
		typeService.getDao().save("type", type);
		
		SessionUtil.refreshSidebar();
		
		return SUCCESS;
	}
	
	public String toInsert(){
		List<Type> typeList = typeService.getDao().find("type", null);
		this.put("typeList", typeList);
		return SUCCESS;
	}
	
	public String toUpdate(){
		type = typeService.getDao().findOne("type", type);
		
		List<Type> typeList = typeService.getDao().find("type", null);
		this.put("typeList", typeList);
		
		return SUCCESS;
	}
	
	public String update(){
		typeService.getDao().update("type", type);
		
		SessionUtil.refreshSidebar();
		
		return SUCCESS;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public TypeService getTypeService() {
		return typeService;
	}

	public void setTypeService(TypeService typeService) {
		this.typeService = typeService;
	}


}
